package com.chendx.store.order.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.order.dto.ReturnOrderDto;
import com.chendx.model.order.pojo.ReturnCause;
import com.chendx.store.api.order.ReturnOrderControllerApi;
import com.chendx.store.order.service.ReturnCauseService;
import com.chendx.store.order.service.ReturnOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chendx on 2022/4/21.
 */
@RestController
@RequestMapping("/api/order")
public class ReturnOrderController implements ReturnOrderControllerApi {

    @Autowired
    private ReturnOrderService returnOrderService;
    @Autowired
    private ReturnCauseService returnCauseService;

    @Override
    @PostMapping("/return")
    public ResponseResult returnOrder(@RequestBody ReturnOrderDto dto) {
        return returnOrderService.returnOrder(dto);
    }

    @Override
    @GetMapping("/returnDetail")
    public ResponseResult returnDetail(String id) {
        return returnOrderService.returnDetail(id);
    }

    @Override
    @GetMapping("/causeList")
    public ResponseResult returnCauseList(String type) {
        if (StringUtils.isEmpty(type)){
            type = "1";
        }
        List<ReturnCause> list = returnCauseService.list(Wrappers.<ReturnCause>lambdaQuery().eq(ReturnCause::getStatus, "0").eq(ReturnCause::getType, type));
        return ResponseResult.okResult(list);
    }

    @Override
    @PostMapping("/closeReturn")
    public ResponseResult closeReturn(@RequestBody ReturnOrderDto dto) {
        return returnOrderService.close(dto);
    }
}
